package com.yulebaby.callsenter.task;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.lzy.okgo.model.HttpParams;
import com.yulebaby.callsenter.bean.CustomerBean;
import com.yulebaby.callsenter.bean.FindCustomerInSeaBean;
import com.yulebaby.callsenter.database.manager.Dao;
import com.yulebaby.callsenter.event.SequenceEvent;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.http.ServerUrls;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.http.callback.ResultCallback;
import com.yulebaby.callsenter.ui.activity.LoginActivity;
import com.yulebaby.callsenter.utils.GsonUtil;
import com.yulebaby.callsenter.utils.SPUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Zhangbo
 * Time: 2016/11/16
 */
public class RequestCustomerTask {

    private static List<CustomerBean> customerByReturn = new ArrayList<>();

    /**
     * 获取所有客户数据
     *
     * @param activity         获取信息的Acitivty
     * @param isEnforceRefrush 是否强制刷新
     */
    public static void doRequestAllCustomer(Activity activity, boolean isEnforceRefrush, int type, ResultCallback callback) {
        customerByReturn.clear();
        if (isEnforceRefrush) {
            HttpParams httpParams = getHttpParams(activity);
            httpParams.put("updateTime", Dao.getInstance().getUpdateTime());
            requestData(activity, httpParams, ServerUrls.OPT_CUSTOMER, callback);
        } else {
            if (type == SequenceEvent.sequenceByTime) {
                customerByReturn.addAll(Dao.getInstance().listCustomerBySaler());
            } else if (type == SequenceEvent.sequenceByLevel) {
                customerByReturn.addAll(Dao.getInstance().listCustomerByImportent());
            }
            if (customerByReturn.size() == 0) {
                HttpParams httpParams = getHttpParams(activity);
                httpParams.put("updateTime", 0l);
                requestData(activity, httpParams, ServerUrls.OPT_CUSTOMER, callback);
            } else {
                callback.onResult(customerByReturn);
            }
        }
    }

    /**
     * 获取近期回访客户数据
     *
     * @param activity         获取信息的Acitivty
     * @param isEnforceRefrush 是否强制刷新
     */
    public static void doRequestVisitCustomer(Activity activity, boolean isEnforceRefrush, ResultCallback callback) {
        customerByReturn.clear();
        customerByReturn.addAll(Dao.getInstance().listCustomerByReturn());
        callback.onResult(customerByReturn);
        /*if (isEnforceRefrush == true) {
            requestData(activity, getHttpParams(), ServerUrls.OPT_VISITCUSTOMER, callback);
        } else {
            customerByReturn.addAll(Dao.getInstance().listCustomerByReturn());
            if (customerByReturn.size() == 0) {
                requestData(activity, getHttpParams(), ServerUrls.OPT_VISITCUSTOMER, callback);
            } else {
                callback.onResult(customerByReturn);
            }
        }*/
    }


    /**
     * 请求数据
     *
     * @param activity
     * @param Url
     * @param callback
     */

    private static void requestData(Activity activity, HttpParams params, String Url, final ResultCallback callback) {
        customerByReturn.clear();
        HttpHelper.getInstance().get(activity, Url, params, new HttpCallback<List<CustomerBean>>() {
            @Override
            public void onSuccess(List<CustomerBean> result) {
                for (CustomerBean customerBean : result) {
                    transCustomerBean(customerBean);
                }
                Dao.getInstance().insertMultCustomerBean(result);
                customerByReturn.clear();
                customerByReturn.addAll(result);
                callback.onResult(customerByReturn);
            }

            @Override
            public void onFail(String exception) {
                super.onFail(exception);
                customerByReturn.clear();
                callback.onResult(null);
            }

            @Override
            public void onFailParam(String info) {
                super.onFailParam(info);
                customerByReturn.clear();
                callback.onResult(customerByReturn);
            }
        });
    }

    /**
     * 对CustomerBean 进行转换
     *
     * @param customerBean
     */
    private static void transCustomerBean(CustomerBean customerBean) {
        switch (customerBean.getStage()) {
            case Constant.Stage.CLIENTCONFIRM:
                customerBean.setStep("客户确认");
                break;
            case Constant.Stage.NEWINROOM:
                customerBean.setStressIntent(Constant.StressIntent.STATE_NEWINROOM);
                customerBean.setStep("新入库");
                return;
            case Constant.Stage.COMMUNICATION:
                customerBean.setStep("沟通中");
                break;
            case Constant.Stage.VISITED:
                customerBean.setStep("已到访");
                break;
            case Constant.Stage.SIGNED:
                customerBean.setStep("已签约");
                break;
            case Constant.Stage.RECISSION:
                customerBean.setStep("已解约");
                break;
        }
        switch (customerBean.getIntention() == null ? "" : customerBean.getIntention()) {
            case "强烈":
                customerBean.setStressIntent(Constant.StressIntent.STATE_STRONG);
                break;
            case "准强烈":
                customerBean.setStressIntent(Constant.StressIntent.STATE_MIDDLE);
                break;
            case "中等":
                customerBean.setStressIntent(Constant.StressIntent.STATE_COMMONLY);
                break;
            case "较弱":
                customerBean.setStressIntent(Constant.StressIntent.STATE_LOW);
                break;
            default:
                customerBean.setStressIntent(Constant.StressIntent.STATE_NONE);
                break;
        }
    }

    /**
     * 根据销售ID获得即将投公海客户的天数
     *
     * @param activity         获取信息的Acitivty
     * @param isEnforceRefrush 是否强制刷新
     */
    public static void doGoSeaCustomer(Activity activity, boolean isEnforceRefrush, ResultCallback callback) {
        customerByReturn.clear();
        customerByReturn.addAll(Dao.getInstance().listCustomerByDays());
        callback.onResult(customerByReturn);

        /*if (isEnforceRefrush == true) {
            requestData(activity, getHttpParams(), ServerUrls.OPT_GOSEACUSTOMERINFO, callback);
        } else {
            customerByReturn.addAll(Dao.getInstance().listCustomerByDays());
            if (customerByReturn.size() == 0) {
                requestData(activity, getHttpParams(), ServerUrls.OPT_GOSEACUSTOMERINFO, callback);
            } else {
                callback.onResult(customerByReturn);
            }
        }*/
    }

    /**
     * 设置一个总的HttpParams
     *
     * @return
     */
    static private HttpParams getHttpParams(Activity activity) {
        HttpParams httpParams = new HttpParams();
        String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
        int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
        httpParams.put("token", user_token);
        httpParams.put("sale_id", user_id);
        checkNull(user_token, activity);
        return httpParams;
    }


    /**
     * 获取本地客户数据byPhone
     *
     * @param activity       获取信息的Acitivty
     * @param phoneNumOrName 电话号码
     * @param callback       回调
     */
    public static void doRequestCustomerByPhone(Activity activity, String phoneNumOrName, HttpCallback callback) {
        CustomerBean customerBean = Dao.getInstance().queryBeanByPhone(phoneNumOrName);
        if (customerBean != null) {
            callback.onSuccess(customerBean);
        } else {
            callback.onFailParam("没有这个客户号码");
        }


/*        else { //不走网络查找
            mActivity = activity;
            HttpParams byPhoneHttpParams = getHttpParams();
            byPhoneHttpParams.put("phone", phoneNum);
            HttpHelper.getInstance().get(activity, ServerUrls.OPT_CUSTOMERBYPHONE, byPhoneHttpParams, httpCallback);
        }*/
    }

    /**
     * 获取公海客户数据byPhone
     *
     * @param activity    获取信息的Acitivty
     * @param phoneOrName 电话号码
     * @param callback    回调
     */
    public static void doRequestCustomerInSeaByPhone(Activity activity, String phoneOrName, int pagerNum, final HttpCallback callback) {
        HttpParams byPhoneHttpParams = getHttpParams(activity);
        String customer = GsonUtil.parseBeanToJson(new FindCustomerInSeaBean(phoneOrName, "", "", ""));
        byPhoneHttpParams.put("datanum", pagerNum);
        byPhoneHttpParams.put("customer", customer);

        HttpHelper.getInstance().get(activity, ServerUrls.OPT_FINDCUSTOMERINSEA, byPhoneHttpParams, new HttpCallback<List<CustomerBean>>() {
            @Override
            public void onSuccess(List<CustomerBean> result) {
                for (CustomerBean customerBean : result) {
                    transCustomerBean(customerBean);
                }
                callback.onSuccess(result);
            }

            @Override
            public void onFailParam(String info) {
                super.onFailParam(info);
                callback.onFailParam("客户不存在");
            }
        });
    }


    /**
     * 判断是否为空
     *
     * @param string
     * @return
     */
    private static boolean checkNull(String string, Activity mActivity) {
        if (TextUtils.isEmpty(string)) {
            ToastUtil.showToast("登录信息出错,请重新登录");
            //TOKEN 失效跳转到登录界面
            SPUtil.remove(Constant.USER_TOKEN); //对登录信息进行清除
            SPUtil.remove(Constant.USER_ID);
            if (mActivity != null) {
                mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
                mActivity.finish();
            }
            return false;
        }
        return true;
    }
}
